home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4031 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: simple code, argc, argv, strcmp()
  5. Date: Thu, 01 Feb 1996 12:50:27 GMT
  6. Organization: Netcom
  7. Message-ID: <3110b5eb.61577024@nntp.ix.netcom.com>
  8. References: <11f7cc$17261a.3b3@daprez>
  9. NNTP-Posting-Host: ix-dc9-28.ix.netcom.com
  10. X-NETCOM-Date: Thu Feb 01  4:50:50 AM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. otisg@panther.middlebury.edu (Otis Gospodnetic) wrote:
  14.  
  15. > Hi, a C question...
  16. > this one has been bothering ALL afternoon !
  17. > This snippet of code is supposed to call appropriate function based on what
  18. > command line arguments are supplied.
  19. > usage: <program> -e [SourceFile] RemoteFile
  20. >         or
  21. >        <program> -d [InFile]
  22. > if the person doesn't use this right the program is supposed to call Usage()
  23. > which is not here, but it's in my code, and if the person calls the program
  24. > the correct way one of the two actions should be taken (encode or decode).
  25. > Problem - I am doing something wrong and I almost always end up calling
  26. > Usage() even if I use the program correctly.
  27. > #include <stdio.h>
  28. > #include <stdlib.h>
  29. > #include <string.h>
  30. > int main (int argc, char **argv) {
  31. >   void Usage   (void);
  32. >   void Encode (int, char **);
  33. >   void Decode (int, char **);
  34. >   if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
  35. >     Usage();
  36. >   }
  37. >   if (strcmp(argv[1],"-d") && argc < 3) {
  38. >     printf ("D %i\n", argc);
  39. >     Usage();
  40. >   }
  41. >   if (strcmp(argv[1],"-e") && argc < 4) {
  42. >     printf ("E %i\n", argc);
  43. >     Usage();
  44. >   }
  45. >   if (strcmp(argv[1],"-d") && argc == 3) {
  46. >     Decode(argc, argv);
  47. >   }
  48. >   if (strcmp(argv[1],"-e") && argc == 4) {
  49. >     Encode(argc, argv);
  50. >   }
  51. >   return 0;
  52. > }
  53. > Can someone see what's wrong with this ?
  54. > am I not using strcmp() right ?
  55. > Thanks a bunch (I don't have any more hair to pull from my head !)
  56.  
  57. You've got a few problems.
  58.  
  59. When you think there's a problem with your use of a function, the
  60. first step should be to read the manual and find out what it really
  61. does.  strcmp() returns 0 if the strings are equal, negative if the
  62. first compares less than the second, positive if the first compares
  63. greater.  You seem to be assuming it will return nonzero if they are
  64. equal.
  65.  
  66. Even when you correct that, your first test will be incorrect.  Think
  67. about what it is doing -- you seem to be trying to test whether the
  68. first command line argument is either not equal to -d or not equal to
  69. -e.  This will be true for all possible command line arguments.
  70.  
  71. Finally, think about what happens if there are too many command line
  72. arguments.  You try to call Usage() if there are too few and call the
  73. appropriate function if there are the right number, but simply return
  74. if there are too many.
  75.  
  76.  
  77. Michael M Rubenstein
  78.